import { CoolLocalStorage } from './../../../../common/coolStorage/cool-local-storage'
import { CommonUtil } from 'src/app/common/utils/common.util'
import { Toast, Modal } from 'ng-zorro-antd-mobile'
import { ActivatedRoute, Router } from '@angular/router'
import { Component, OnInit, ViewChild, ViewChildren } from '@angular/core'
import { Location } from '@angular/common'
import { DetailService } from './services/detail.service'
import { ImgUploadComponent } from 'src/app/common/components/img-upload/img-upload.component'

@Component({
  selector: 'app-estate-detail',
  templateUrl: './estate-detail.component.html',
  styleUrls: ['./estate-detail.component.scss'],
  providers: [Modal]
})
export class EstateDetailComponent implements OnInit {
  public loading: boolean = true
  public detail: any
  public projectID: any
  public ishowApply: boolean = false
  public error: boolean = false
  public Notice = {
    show: false,
    imgUrl: ''
  }

  items = ['楼盘', '活动', '动态', '户型', '点评']
  currentIndex = 0
  arroundMatching = [
    { alias: 'bus', icon: 'icon_bus.png', name: '公交' },
    { alias: 'school', icon: 'icon_school.png', name: '学校' },
    { alias: 'restaurant', icon: 'icon_restaurant.png', name: '餐饮' },
    { alias: 'shopping', icon: 'icon_shopping.png', name: '购物' },
    { alias: 'hospital', icon: 'icon_hospital.png', name: '医院' }
  ]
  estates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

  public imgview = {
    show: false,
    list: [],
    index: 0
  }

  public reviewMd = {}
  public mapOption={
    type:"mini",
    latng:{
        lat:"",
        lng:""
    },
    name:""
  }

  constructor(
    private location: Location,
    private detailService: DetailService,
    private routeInfo: ActivatedRoute,
    private router: Router,
    private commonUtil: CommonUtil,
    private coolLocalStorage: CoolLocalStorage,
    private _toast: Toast,
    private _modal: Modal
  ) {}

  ngOnInit() {
    let id = this.routeInfo.snapshot.queryParams['project']
    this.projectID = id
    this.getEstateDetail(id)
  }

  routerMap(key){
      this.router.navigate(['/workspace/estate/estateQqMap'], {
        queryParams: {
            lat:this.mapOption.latng.lat,
            lng: this.mapOption.latng.lng,
            key:key,
            name:this.detail.Name
        }
    });
  }

  //我要点评
  review() {
    this.Notice.show = true
    this.reviewMd = {
      title: '我要点评',
      handok: e => {
        let _photo: any
        if (e.photo) {
          _photo = JSON.stringify([{ Category: 'Comment', Index: 0, Items: e.photo }])
        } else {
          _photo = ''
        }

        let req = {
          project: this.projectID,
          content: e.textarea,
          anonymous: e.checked,
          photo: _photo
        }
        this.detailService.review(req, res => {
          console.log(res)
          this.Notice.show = false
        })
      }
    }
  }

  //我要提问
  questionMe() {
    this.Notice.show = true

    this.reviewMd = {
      title: '我要提问',
      handok: e => {
        let req = {
          project: this.projectID,
          content: e.textarea,
          anonymous: e.checked
        }
        this.detailService.question(req, res => {
          console.log(res)
          this.Notice.show = false
        })
      }
    }
  }
  //报名
  applyUp(item) {
    console.log(item)

    // this.ishowApply = true;
    this.router.navigate(['/workspace/home/activitySignup'], { queryParams: { ActivityID: item.Id } })
  }

  applyClose() {
    this.ishowApply = false
  }

  onClickLeft() {
    this.location.back()
  }

  afterChange(number) {
    this.currentIndex = number

    // console.log(this.detail.Photos);
    if (this.currentIndex === 0 || this.currentIndex === 7) {
      this.router.navigate(['/workspace/estate/images'], { queryParams: { project: this.projectID } })
    }
  }

  //置顶
  scrollSpeed() {
    window.scrollTo(0, 0)
  }

  //api
  getEstateDetail(id) {
    let reqdata = {
      project: id,
      zone: this.coolLocalStorage.getItem('zone')
    }
    this.loading = true

    this.detailService.getEstateDetail(reqdata, res => {
      if (res.Success && res.Result) {
        let items = res.Result
        let photo = JSON.parse(items.Photo)

        if (items.PromotionSummary) {
          items.PromotionSummary.Description = JSON.parse(items.PromotionSummary.Description)
          if (items.PromotionSummary.Description.length > 0) {
            items.PromotionSummary.Description.forEach(element => {
              if (element.Name === '活动详情') {
                items.PromotionSummary.Description = element.Value
              }
            })
          } else {
            items.PromotionSummary.Description = ''
          }
        }

        if (items.ArtcleSummarys[0] != null) {
          if (items.ArtcleSummarys[1] != null) {
            items.ArtcleSummarys[1]['Photo'] = this.commonUtil.getThumbnail(items.ArtcleSummarys[1]['Photo'])
          }
        }

        items.LayoutSummarys.forEach(element => {
          element.PhotoSrc = JSON.parse(element.Photo)
          element.Photo = this.commonUtil.getThumbnail(element.Photo)
        })

        if (items.CommentSummary) {
          if (items.CommentSummary.Nick.Photo) {
            items.CommentSummary.NickPhoto = this.commonUtil.getThumbnail(items.CommentSummary.Nick.Photo)
          } else {
            items.CommentSummary.Photo = ''
          }
        }

        items.ProjectSummarys.forEach(item => {
          let Photo = this.commonUtil.getThumbnail(item.Photo)
          let Tag = this.commonUtil.getTag(item.Tag)
          item.Photo = Photo
          item.Tag = Tag
          if (item.Landmark && item.Landmark.indexOf('[') > -1) {
            item.Landmark = JSON.parse(item.Landmark)[0]
          }
          return item
        })

        this.detail = items
        this.detail['Photos'] = []

        if (items.Tag) {
          this.detail['Tag'] = JSON.parse(items.Tag)
        }

        //相册
        photo.forEach(element => {
          if (element.Category) {
            element.Items.forEach(el => {
              let o = {
                src: el,
                Category: element.Category
              }
              this.detail.Photos.push(o)
            })
          }
        })

        //户型图
        items.LayoutSummarys.forEach(element => {
          // let _photo = JSON.parse(element.Photo);
          element.PhotoSrc.forEach(elm => {
            if (elm.Category) {
              elm.Items.forEach(el => {
                let o = {
                  src: el,
                  Category: elm.Category
                }
                this.detail.Photos.push(o)
              })
            }
          })
        })


        //经纬度
        this.mapOption.latng.lat=items.Latitude
        this.mapOption.latng.lng=items.Longitude
        this.mapOption.name=this.detail.Name;



        this.error = false


      } else {
        this.error = true
      }

      this.loading = false
      this.scrollSpeed()
    })
  }
}
